// @ts-nocheck
import router from '@system.router';
import BrightnessModel from '../../model/brightnessImpl/BrightnessModel';
import SystemBrightnessMode from '../../model/brightnessImpl//SystemBrightnessModel'
import BaseParseConfModel from '../../model/baseParseConfImpl/BaseParseConfModel.js'
import LogUtil from '../../common/baseUtil/LogUtil.js';

let brightnessModel = new BrightnessModel()
let systemBrightness = new SystemBrightnessMode
let logUtil = new LogUtil()
let baseParseConfModel = new BaseParseConfModel
let list = [{
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 1,
    "settingAlias": "getValue",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
}, {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 1,
    "settingAlias": "getMode",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
}, {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 1,
    "settingAlias": "setMode",
    "settingTitle": "",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
}, {
    "settingIcon": "/res/image/icon_batterySOC.png",
    "settingType": 1,
    "settingAlias": "setKeepScreenOn",
    "settingTitle": "超时灭屏",
    "settingValue": "",
    "settingArrow": "/res/image/ic_settings_arrow.png",
    "settingShowDivider": true,
}]

export default {
    data: {
        bright: null,
        brightness: null,
        mode: 0,
        keepScreenOn: false,
        before: '/res/image/brightness_first.png',
        after: '/res/image/brightness_second.png',
        brightnessJsonPath: "/data/app/el1/bundle/public/com.example.powerhap/com.example.entry/assets/power/resources/rawfile/brightnessList.json",
        brightnessList: [],
    },
    onInit() {
        console.info('BrightnessPage onInit start:')
        this.brightnessList = baseParseConfModel.getJsonData(this.brightnessJsonPath)
        this.brightnessList = this.brightnessList || list
        console.info('setting BrightnessPage get by JSON' + JSON.stringify(this.brightnessList))
        for (let key in this.brightnessList) {
            var settingAlias = this.brightnessList[key].settingAlias;
            this.brightnessList[key].settingTitle = this.$t('strings.'.concat(settingAlias))
        }
        this.getValue();
        this.getMode();
        logUtil.info('BrightnessPage onInit end:')
    },
    updateData (call) {
        for (let key in this.brightnessList) {
            call(this.brightnessList[key])
        }
    },
    getValue () {
        logUtil.info('BrightnessPage getValue begin')
        systemBrightness.getValue((value) => {
            this.bright = value.value
            console.info('BrightnessPage bright is: ' + this.bright)
        });
        this.updateData((listValue) => {
            if (listValue.settingAlias == "getValue") {
                logUtil.info('BrightnessPage update bright is: ' + this.bright)
                listValue.settingValue = this.bright
            }
        })
    },
    setValue (data) {
        logUtil.info('BrightnessPage setValue end data.value：' + data.value)
        this.bright = data.value
        brightnessModel.setBrightness(this.bright)
        logUtil.info('BrightnessPage setValue end this.bright：' + this.bright)
        this.getValue()
    },
    setObjValue (data) {
        this.bright = data.value
        systemBrightness.setValue(this.bright)
        logUtil.info('BrightnessPage setObjValue end this.bright：' + this.bright)
        this.getValue()
    },

    changeValue(event) {
        logUtil.info('changeValue onclick start: ' + JSON.stringify(event.detail.text.settingAlias))
        switch (event.detail.text.settingAlias) {
            case "getMode":
                this.getMode()
                break;
            case "setMode":
                this.setMode()
                break;
            case "setKeepScreenOn":
                this.setKeepScreenOn()
                break;
            default:
                break;
        }
    },
    getMode() {
        console.info('getMode')
        systemBrightness.getMode((value) => {
            console.info('getMode mode: ' + value.mode)
            this.mode = value.mode
            this.updateData((listValue) => {
                if (listValue.settingAlias == "getMode") {
                    listValue.settingValue = this.mode ? "自动调节" : "手动调节"
                }
            })
            console.info('BrightnessPage mode is: ' + this.mode)
        })
    },
    setMode() {
        let mode = this.mode ? 0 : 1
        console.info('setMode: ' + mode)
        systemBrightness.setMode(mode)
        this.getMode()
    },
    setKeepScreenOn() {
        console.info('setKeepScreenOn: ' + this.keepScreenOn)
        systemBrightness.setKeepScreenOn(!this.keepScreenOn, (keep) => {
            console.info('setKeepScreenOn success: ' + keep)
            this.updateData((listValue) => {
                if (listValue.settingAlias == "setKeepScreenOn") {
                    this.keepScreenOn = keep
                    listValue.settingValue = this.keepScreenOn ? "屏幕常亮" : "超时灭屏"
                }
            })
        })
    },
    pullDown() {
        this.getValue();
        this.getMode();

        brightnessModel.setBrightnessError();
    },
    back() {
        router.back()
    },
    onBackPress() {
        router.back()
    },
    onCreate() {
        logUtil.info('BrightnessPage onCreate')
    },
    onReady() {
        logUtil.info('BrightnessPage onReady')
    },
    onShow() {
        this.getValue()
        logUtil.info('BrightnessPage onShow')
    },
    onHide() {
        logUtil.info('BrightnessPage onHide')
    },
    onDestroy() {
        if(this.keepScreenOn) {
            this.setKeepScreenOn()
        }
        logUtil.info('BrightnessPage onDestroy')
    }
}